#include <iostream>
#include <set>

using namespace std;
//set容器排序:set容器默认排序规则为从小到大，改变排序规则
//利用仿函数，可以改变排序规则

//set存放自定义数据类型
class Person {
public:
    string name;
    int age;

    Person(string name, int age) {
        this->name = name;
        this->age = age;
    }
};

class comparePerson {
public:
    bool operator()(const Person &p1, const Person &p2) {
        //按照年龄进行排序  降序
        return p1.age > p2.age;
    }
};

void test() {
    set<Person, comparePerson> s;

    Person p1("刘备", 23);
    Person p2("关羽", 27);
    Person p3("张飞", 25);
    Person p4("赵云", 21);

    s.insert(p1);
    s.insert(p2);
    s.insert(p3);
    s.insert(p4);

    for (set<Person, comparePerson>::iterator it = s.begin(); it != s.end(); it++) {
        cout << "name:" << it->name << ",age:" << it->age << endl;
    }
}

int main() {
    test();
    return 0;
}